package edu.flying.panda.taskmanager;

import java.util.ArrayList;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

import android.widget.ListView;

/**
 * Home Screen which displays Tasks and menus
 * @author FlyingPanda
 */

public class HomeScreen extends Activity {
	
	
	private ListView list;
    private MyListAdapter adapter;
    private String filter;
    private Date due;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_screen);
		Log.d("HS","h1");
		final Button logOut_button = (Button) findViewById(R.id.home_screen_back_button);
		final Button addTask_button = (Button) findViewById(R.id.home_screen_addTask_button);
		final Button sort_button = (Button) findViewById(R.id.home_screen_sort_button);
		Log.d("HS","h1.5");
		Bundle extras = getIntent().getExtras();
		
		Log.d("HS","h1.75");
		/**
		 * Sets up the list to display all tasks
		 */
		
		if(extras != null){
			filter = extras.getString("filter");
	
			due = new Date( extras.getString("date") );

		}else{
			filter = "all";
		}
		Log.d("HS","h2");
		Log.d("HS",filter);
		
		ArrayList<Task> ar;
		Log.d("HS","h212");
		
		if(filter.equals("work")){
			ar = TaskManagerActivity.DB.getAllTasks("work");
		}else if(filter.equals("school")){
			ar = TaskManagerActivity.DB.getAllTasks("school");
		}else if(filter.equals("personal")){
			ar = TaskManagerActivity.DB.getAllTasks("personal");
		}else if(filter.equals("before")){
			Log.d("HS","before1");
			ar = TaskManagerActivity.DB.getBefore(due);
			Log.d("HS","before2");
		}else if(filter.equals("after")){
			Log.d("HS","after1");
			ar = TaskManagerActivity.DB.getAfter(due);
			Log.d("HS","after2");
		}else if(filter.equals("completion")){//completed
			ar = TaskManagerActivity.DB.getAllCompletedTasks();
		}else {
			ar = TaskManagerActivity.DB.getAllTasks();
		}
		
		
		
		Log.d("HS","h2.5");
		Task[] tasks = new Task[ar.size()];
		Log.d("HS","h2.75");
		ar.toArray(tasks);
		if(tasks.length>=1)
			Log.d("HS",tasks[0].toString());
		Log.d("HS","h2");
		list=(ListView)findViewById(R.id.list);
		Log.d("HS","h3");
        adapter=new MyListAdapter(this, tasks);
        Log.d("HS","h4");
        list.setAdapter(adapter);
        Log.d("HS","h5");
        
        
        list.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
                  
            	Log.d("HS","h6");
                  Intent intent = new Intent(v.getContext(), EditTaskScreen.class);
                  Log.d("HS","h7");
                 
                  intent.putExtra("task", (Integer)((TextView)v.findViewById(R.id.label)).getTag());
                  Log.d("HS","h8");
                  startActivity(intent);
                  
            }
		
			
      });
        
        
        
		
		/**
		 * Listener for Logout button, returns to Login screen
		 */
		logOut_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(v.getContext(), LogInScreen.class);
				startActivity(myIntent);
				
				

			}
		});
		
		
		/**
		 * Listener for Sort button, goes to Sort Task screen
		 */
		sort_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				 Log.d("HS","ss1");
				Intent myIntent = new Intent(v.getContext(), SortScreen.class);
				 Log.d("HS","ss2");
				myIntent.putExtra("filter", filter);
				 Log.d("HS","ss3");
				startActivity(myIntent);
				 Log.d("HS","ss4");

			}
		});
		

		/**
		 * Listener for AddTask button, goes to New Task screen
		 */
		addTask_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(v.getContext(), AddTaskScreen.class);
				startActivity(myIntent);

			}
		});

	}

}
